package com.example.chinesechess.pieces;

import java.util.List;

import com.example.chinesechess.misc.PlayerEnum;
import com.example.chinesechess.misc.Position;



public abstract class Piece {
	
	//current position of the piece
	protected Position position;
	
	//Which player the piece belong to (PlayerEnum.RED or PlayerEnum.BLACK
	protected PlayerEnum player;
	
	//A List of Position this piece is able to move to in 1 move,
	//which is not accupied by any other pieces, opponent or ally pieces
	protected List<Position> emptyMoves;
	
	//A List of pieces this piece can capture in 1 move
	// all must belong to opponent pieces
	protected List<Piece> possibleCaptures;
	
	//accessor
	public Position getPosition() {
		return position;
	}
	public PlayerEnum getPlayer() {
		return player;
	}
	
	//constructor
	public Piece(Position position, PlayerEnum player) {
		this.position = position;
		this.player = player;
	}
	
	/*
	 * initialise the list of possibleCaptures and empty Moves
	 * @param List of {@link Piece} in play
	 */
	public abstract void initialise(List<Piece> pieces);
	/*
	 * Move the piece to a {@link Position}
	 * @param position
	 * @param List of all {@link Piece} in play
	 * @return the captured {@link Piece} or null
	 */
	public abstract Piece moveTo(Position position, List<Piece> pieces);
	
	public boolean isMovableTo(Position position) {
		for(Position p: this.emptyMoves){
			if(p.x == position.x && p.y == position.y){
				return true;
			}
		}
		for(Piece p:this.possibleCaptures) {
			if(p.getPosition().x== position.x && p.getPosition().y == position.y){
				return true;
			}
		}
		return false;
	}

	public String toString() {
		return (player == PlayerEnum.RED ? "Player: RED" : "Player: BLACK") + " Position: " +position.toString();
	}
	
}
